package com.hnx.admin.common.security.fliter;

import cn.hutool.extra.spring.SpringUtil;
import com.hnx.admin.api.security.core.SecurityPropertyAPI;
import com.hnx.admin.common.security.core.SecurityProperties;
import com.hnx.admin.common.security.utils.RequestParamsWapperUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import java.io.IOException;
import java.util.Map;

/*
创建人： 倔强的头发
创建时间：2024/7/5
功能描述:

*/
public class LoginFilter extends UsernamePasswordAuthenticationFilter implements SecurityPropertyAPI {
    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        if (!request.getMethod().equalsIgnoreCase("post")) {
            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
        }
        SecurityProperties securityProperties = (SecurityProperties) SpringUtil.getBean(SecurityProperties.class);
        if (request.getServletPath().equalsIgnoreCase(securityProperties.getProcessesUrl())) {
            try {
//                Map<String, Object> loginParam = RequestParamsWapperUtil.getLoginParam(request);
//                if(ObjectUtil.isEmpty(loginParam)){
//                    throw new AuthenticationServiceException(LOGIN_REQUEST_EMPTY);
//                }
//                RequestContextHolderUtil.setContext(loginParam);
//                String username = (String) loginParam.get(securityProperties.getUsername());
//                if(ObjectUtil.isEmpty(username)){
//                    throw new UserBlankException(USER_EMPTY_ERROR);
//                }
//                RedisCore redisCore = SpringUtil.getBean(RedisCore.class);
//                Integer cacheObject = redisCore.getCacheObject(PWD_ERR_CNT_KEY + username, Integer.class);
//                if(ObjectUtil.isNotEmpty(cacheObject) && cacheObject >= securityProperties.getMaxRetryCount()){
//                    throw new UserLimitException(RETRY_ERROR_COUNT);
//                }

                Map<String, Object> stringObjectMap = RequestParamsWapperUtil.requestParam(request, securityProperties);
                UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(stringObjectMap.get(securityProperties.getUsername()), stringObjectMap.get(securityProperties.getPassword()));
                setDetails(request, authRequest);
                return this.getAuthenticationManager().authenticate(authRequest);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return super.attemptAuthentication(request, response);
    }
}
